<?php
namespace Seller\Controller;
use Seller\Model\TempItemModel;
use Think\AjaxPage;
use Think\Controller;

class DiyController extends CommonController
{
    public $login_sp_id;

    public function _initialize()
    {
        $params = array("item_mods","ajax_goods_list");
        parent::_initialize($params);
        $this->login_sp_id = session("shop_id");
    }

    // 跳转链接
    public static function get_link( $link_key )
    {
        $link_arr = array(
            "javascript:;", U("Home/Setting/personal") ,U("Home/Index/index") ,U("Home/Good/cate") ,'' ,''
        );
        return $link_arr[$link_key];
    }

    public function index()
    {
        $temp_d = new TempItemModel();
        $items = M("temp_item")->where("is_open = 1")->select();
        $temps = M("temp_diy")->where("shop_id = %d" ,array($this->login_sp_id))->order("temp_sort asc")->select();
        $temps_html = $temp_d->createHtml($temps);
        $this->assign("items" ,$items);
        $this->assign("temps_html" ,$temps_html);
        $this->display();
    }

    // 模块配置项
    public function item_mods()
    {
        $item_key = I("post.itm_k");
        $allow_keys = array("goods" ,"blank", "ad", "banner", "diy" ,"line" ,"nav" ,"cats" ,"search" ,"notice" ,"coupon");
        if(!in_array($item_key ,$allow_keys)){
            $this->ajaxReturn(array("status" => "error","msg" => "键值出错"));
        }
        $html = D("TempItem")->$item_key();
        $this->ajaxReturn(array("status" => "success","itm" => $html));
    }

    // 页面设置
    public function page_set()
    {
//        pre($_POST);exit;
        $page_code = I("post.page_code");
        $position_mod = I("post.position_mod");
        $sorts = array();
        if($position_mod != ""){
            $sorts = array_unique(explode("," ,$position_mod));
        }
        M("temp_diy")->where("page_code = '%s' and shop_id = %d", array($page_code ,$this->login_sp_id))->delete();
        if(empty($sorts)){
            $this->success("设置完成" ,U("Diy/index") ,1);
        }else{
            $link_arrs = array(1, 2 );
            $add_items["page_code"] = $page_code;
            $add_items["shop_id"] = $this->login_sp_id;
            foreach($sorts as $kst => $st){
                $mod_set_node = I("post.$st");
                $temp_mod = explode("_" ,$st);
                $add_items["temp_sort"] = ($kst + 1);
                $add_items["temp_code"] = $temp_mod[0];
                // 广告图
                if($temp_mod[0] == "ad"){
                    if(in_array($mod_set_node["jump_link"] ,$link_arrs)){
                        $mod_set_node["linkurl"] = self::get_link($mod_set_node["jump_link"]);
                        unset($mod_set_node["cate"]);
                        unset($mod_set_node["goods"]);
                        unset($mod_set_node["cname"]);
                        unset($mod_set_node["gname"]);
                        unset($mod_set_node["out_link"]);
                    }
                    if($mod_set_node["jump_link"] == 3){
                        $mod_set_node["linkurl"] = U("Home/Good/agentgoods" ,array("id" => $mod_set_node["cate"]));
                        unset($mod_set_node["goods"]);
                        unset($mod_set_node["gname"]);
                        unset($mod_set_node["out_link"]);
                    }
                    if($mod_set_node["jump_link"] == 4){
                        $mod_set_node["linkurl"] = U("Home/Good/goodsdetail" ,array("id" => $mod_set_node["goods"]));
                        unset($mod_set_node["cate"]);
                        unset($mod_set_node["cname"]);
                        unset($mod_set_node["out_link"]);
                    }
                    if($mod_set_node["jump_link"] == 5){
                        $mod_set_node["linkurl"] = $mod_set_node["out_link"];
                        unset($mod_set_node["cate"]);
                        unset($mod_set_node["goods"]);
                        unset($mod_set_node["cname"]);
                        unset($mod_set_node["gname"]);
                    }
                }
                // 文字标题
                if($temp_mod[0] == "nav"){
                    if(in_array($mod_set_node["jump_link"] ,$link_arrs)){
                        $mod_set_node["linkurl"] = self::get_link($mod_set_node["jump_link"]);
                        unset($mod_set_node["cate"]);
                        unset($mod_set_node["goods"]);
                        unset($mod_set_node["cname"]);
                        unset($mod_set_node["gname"]);
                        unset($mod_set_node["out_link"]);
                    }
                    if($mod_set_node["jump_link"] == 3){
                        $mod_set_node["linkurl"] = U("Home/Good/agentgoods" ,array("id" => $mod_set_node["cate"]));
                        unset($mod_set_node["goods"]);
                        unset($mod_set_node["gname"]);
                        unset($mod_set_node["out_link"]);
                    }
                    if($mod_set_node["jump_link"] == 4){
                        $mod_set_node["linkurl"] = U("Home/Good/goodsdetail" ,array("id" => $mod_set_node["goods"]));
                        unset($mod_set_node["cate"]);
                        unset($mod_set_node["cname"]);
                        unset($mod_set_node["out_link"]);
                    }
                    if($mod_set_node["jump_link"] == 5){
                        $mod_set_node["linkurl"] = $mod_set_node["out_link"];
                        unset($mod_set_node["cate"]);
                        unset($mod_set_node["goods"]);
                        unset($mod_set_node["cname"]);
                        unset($mod_set_node["gname"]);
                    }
                }
                // banner
                if($temp_mod[0] == "banner"){
                    $img_lengths = count($mod_set_node["img"]);
                    $banner_data = array();
                    for($i = 0;$i < $img_lengths; $i ++){
                        $banner_data[$i]["img"] = $mod_set_node["img"][$i];
                        $banner_data[$i]["jump_link"] = $mod_set_node["jump_link"][$i];
                        $banner_data[$i]["cate"] = $mod_set_node["cate"][$i];
                        $banner_data[$i]["cname"] = $mod_set_node["cname"][$i];
                        $banner_data[$i]["goods"] = $mod_set_node["goods"][$i];
                        $banner_data[$i]["gname"] = $mod_set_node["gname"][$i];
                        if(in_array($mod_set_node["jump_link"][$i] ,$link_arrs)){
                            $banner_data[$i]["linkurl"] = self::get_link($mod_set_node["jump_link"][$i]);
                        }
                        if($mod_set_node["jump_link"][$i] == 3){
                            $banner_data[$i]["linkurl"] = U("Home/Good/agentgoods" ,array("id" => $mod_set_node["cate"][$i]));
                        }
                        if($mod_set_node["jump_link"][$i] == 4){
                            $banner_data[$i]["linkurl"] = U("Home/Good/goodsdetail" ,array("id" => $mod_set_node["goods"][$i]));
                        }
                        if($mod_set_node["jump_link"][$i] == 5){
                            $banner_data[$i]["linkurl"] = $mod_set_node["out_link"][$i];
                        }
                    }
                    $mod_set_node = $banner_data;
                }
                // diy
                if($temp_mod[0] == "diy"){
                    $diy_data = array();
                    for($i = 0;$i < $mod_set_node["module"]; $i ++){
                        $diy_data[$i]["img"] = $mod_set_node["img"][$i];
                        $diy_data[$i]["jump_link"] = $mod_set_node["jump_link"][$i];
                        $diy_data[$i]["cate"] = $mod_set_node["cate"][$i];
                        $diy_data[$i]["cname"] = $mod_set_node["cname"][$i];
                        $diy_data[$i]["goods"] = $mod_set_node["goods"][$i];
                        $diy_data[$i]["gname"] = $mod_set_node["gname"][$i];
                        $diy_data[$i]["module"] = $mod_set_node["module"];
                        if(in_array($mod_set_node["jump_link"][$i] ,$link_arrs)){
                            $diy_data[$i]["linkurl"] = self::get_link($mod_set_node["jump_link"][$i]);
                        }
                        if($mod_set_node["jump_link"][$i] == 3){
                            $diy_data[$i]["linkurl"] = U("Home/Good/agentgoods" ,array("id" => $mod_set_node["cate"][$i]));
                        }
                        if($mod_set_node["jump_link"][$i] == 4){
                            $diy_data[$i]["linkurl"] = U("Home/Good/goodsdetail" ,array("id" => $mod_set_node["goods"][$i]));
                        }
                        if($mod_set_node["jump_link"][$i] == 5){
                            $diy_data[$i]["linkurl"] = $mod_set_node["out_link"][$i];
                        }
                    }
                    $mod_set_node = $diy_data;
                }
                // 分类
                if($temp_mod[0] == "cats"){
                    $cats_data = array();
                    for($i = 0;$i < 4; $i ++){
                        $cats_data[$i]["img"] = $mod_set_node["img"][$i];
                        $cats_data[$i]["jump_link"] = $mod_set_node["jump_link"][$i];
                        $cats_data[$i]["cate"] = $mod_set_node["cate"][$i];
                        $cats_data[$i]["cname"] = $mod_set_node["cname"][$i];
                        if($mod_set_node["jump_link"][$i] == 3){
                            $cats_data[$i]["linkurl"] = U("Home/Good/agentgoods" ,array("id" => $mod_set_node["cate"][$i]));
                        }
                        if($mod_set_node["jump_link"][$i] == 5){
                            $cats_data[$i]["linkurl"] = $mod_set_node["out_link"][$i];
                        }
                    }
                    $mod_set_node = $cats_data;
                }
                // goods
                if($temp_mod[0] == "goods"){
                    if(!empty($mod_set_node["c_id"])){
                        $mod_set_node["cats_ids"] = implode("," ,$mod_set_node["c_id"]);
                    }else{
                        $mod_set_node["goods_ids"] = implode("," ,$mod_set_node["g_id"]);
                    }
                    unset($mod_set_node["g_id"]);
                    unset($mod_set_node["c_id"]);
                }
                // 公告
                if($temp_mod[0] == "notice"){
                    $notices_lengths = count($mod_set_node["title"]);
                    $notice_data = array();
                    for($i = 0;$i < $notices_lengths; $i ++){
                        $notice_data[$i]["title"] = $mod_set_node["title"][$i];
                        $notice_data[$i]["jump_link"] = $mod_set_node["jump_link"][$i];
                        if(in_array($mod_set_node["jump_link"][$i] ,$link_arrs)){
                            $notice_data[$i]["linkurl"] = self::get_link($mod_set_node["jump_link"][$i]);
                        }
                        if($mod_set_node["jump_link"][$i] == 5){
                            $notice_data[$i]["linkurl"] = $mod_set_node["out_link"][$i];
                        }
                    }
                    $mod_set_node = $notice_data;
                }
                $add_items["temp_conf"] = serialize($mod_set_node);
                M("temp_diy")->add($add_items);
            }
            $this->success("设置完成" ,U("Diy/index") ,1);
        }
    }

    // AJAX 读商品列表
    public function ajax_goods_list()
    {
        $where = ' shelves = 1 '; // 搜索条件
        $where .= ' and shop_id = '.$this->login_sp_id; // 搜索条件
        // 关键词搜索
        $good_name = I('g_name') ? trim(I('g_name')) : '';
        if($good_name) {
            $where = "$where AND (g_name like '%$good_name%' or good_sn like '%$good_name%')" ;
        }

        $count = M('good')->where($where)->count();
        $Page  = new AjaxPage($count, 8);
        $show = $Page->show();
        $goodsList = M('good')->where($where)->order("create_time desc")->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('goods',$goodsList);
        $this->assign('page',$show);// 赋值分页输出
        $this->display();
    }

    // AJAX 读分类列表
    public function ajax_cates_list()
    {
        $where = ' pid = 0 AND status = 1 '; // 搜索条件
        // $where .= ' and shop_id = '.$this->login_sp_id; // 搜索条件
        // 关键词搜索
        $cate_name = I('cate_name') ? trim(I('cate_name')) : '';
        if($cate_name) {
            $where = "$where AND (c_name like '%$cate_name%')" ;
        }
        $count = M('category')->where($where)->count();
        $Page  = new AjaxPage($count, 8);
        $show = $Page->show();
        $catesList = M('category')->where($where)->order("sort asc")->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('cates',$catesList);
        $this->assign('page',$show);// 赋值分页输出
        $this->display();
    }

    // AJAX 读商品列表 商品diy的专属模块
    public function ajax_choises_list()
    {
        $where = ' shelves = 1 '; // 搜索条件
        $where .= ' and shop_id = '.$this->login_sp_id; // 搜索条件
        // 关键词搜索
        $good_name = I('gname') ? trim(I('gname')) : '';
        if($good_name) {
            $where = "$where AND (g_name like '%$good_name%' or good_sn like '%$good_name%')" ;
        }
        $count = M('good')->where($where)->count();
        $Page  = new AjaxPage($count, 8);
        $show = $Page->show();
        $goodsList = M('good')->where($where)->order("create_time desc")->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('goods',$goodsList);
        $this->assign('page',$show);// 赋值分页输出
        $this->display();
    }

    // AJAX 读商品列表 商品diy的专属模块
    public function ajax_choises_cates()
    {
        $where = ' status = 1 '; // 搜索条件
        // 关键词搜索
        $cname = I('cname') ? trim(I('cname')) : '';
        if($cname) {
            $where = "$where AND (c_name like '%$cname%')" ;
        }
        $count = M('category')->where($where)->count();
        $Page  = new AjaxPage($count, 8);
        $show = $Page->show();
        $catsList = M('category')->where($where)->order("sort asc")->field("id,c_name")
            ->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('catsList',$catsList);
        $this->assign('page',$show);// 赋值分页输出
        $this->display();
    }

    // AJAX 动态添加商品列表
    public function ajax_goods_table()
    {
        $where = ' shelves = 1 '; // 搜索条件
        $where .= ' and shop_id = '.$this->login_sp_id; // 搜索条件
        // 关键词搜索
        $data_type = I('data_type');
        $ids = I('ids');
        $goods_style = I('goods_style');
        if(is_array($ids) && !empty($ids)) {
            $_ids = implode("," ,$ids);
            if($data_type == 1){
                $where = "$where AND id IN ($_ids)" ;
                $limit = "0,100";
            }else{
                $where = "$where AND cate IN ($_ids)" ;
                $limit = $goods_style == 1 ? "0,9" : "0,6";
            }
        }else{
            $where = "$where AND hot = 100" ;
        }
        $goodsList = M('good')->where($where)->order("sale_num desc")->limit($limit)->select();
        $this->assign('goods',$goodsList);
        $this->assign('goods_style',$goods_style);
        $this->display();
    }

    // 图片添加更多
    public function add_more_img()
    {
        $key = I("post.key");
        $sort = I("post.sort");
        $imgs = I("post.img_len");
        $allow_keys = array("goods" ,"blank", "ad", "banner", "diy" ,"line" ,"nav" ,"cats" ,"search" ,"notice" ,"coupon");
        if(!in_array($key ,$allow_keys)){
            $this->ajaxReturn(array("status" => "error","msg" => "键值出错"));
        }
        $html = D("TempItem")->get_img_html( $key."_", $sort, $imgs);
        $this->ajaxReturn(array("status" => "success","itm" => $html));
    }

    // 公告添加更多
    public function add_more_notice()
    {
        $sort = I("post.sort");
        $notices = I("post.notice_len");
        $html = D("TempItem")->notice_list($sort, $notices);
        $this->ajaxReturn(array("status" => "success","itm" => $html));
    }

    // 模块选择
    public function get_module()
    {
        $module = I("post.module");
        $sort = I("post.sort");
        $html = D("TempItem")->get_module("diy_" ,$module ,$sort);
        $this->ajaxReturn(array("status" => "success","itm" => $html));
    }
    // 模块选择
    public function get_module_img()
    {
        $module = I("post.module");
        $sort = I("post.sort");
        $html = D("TempItem")->get_img_show($module ,$sort);
        $this->ajaxReturn(array("status" => "success","itm" => $html));
    }
}